package org.example.app.service;

import jakarta.transaction.Transactional;
import org.example.app.model.BookKeep;
import org.example.app.model.Statistics;
import org.example.app.model.User;
import org.example.app.repository.BookKeepRepository;
import org.example.app.repository.UserRepository;
import org.example.app.service.dto.BookKeepDto;
import org.example.app.web.vm.BookKeepVm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BookKeepService {
    @Autowired
    private BookKeepRepository bookKeepRepository;


    @Autowired
    private UserRepository userRepository;

    @Transactional
    public  BookKeepDto create(BookKeepVm bookKeepVm,Integer userId) {

        bookKeepVm.setUserId(userId);

        Optional<User> user = userRepository.findById(userId);

        if (!user.isPresent()){
            throw  new RuntimeException("用户不存在");
        }

        BookKeep bookKeep = new BookKeep(bookKeepVm,user.get());
        bookKeepRepository.save(bookKeep);
        BookKeepDto bookKeepDto = new BookKeepDto(bookKeep);
        System.out.println(bookKeepDto);

        return bookKeepDto;
    }



    @Transactional
    public List<BookKeepDto> getAccount(User user) {


        List<BookKeep> bookKeepList = bookKeepRepository.findByUser(user);
        List<BookKeepDto> bookKeepDtoList = bookKeepList.stream()
                .map(BookKeepDto::new)
                .toList();
        //将账单列表转换成DTO
//        System.out.println(bookKeepDtoList);

        return bookKeepDtoList;
    }

    public void deleteBookKeep(BookKeep bookKeep) {
        bookKeepRepository.delete(bookKeep);
    }

    public BookKeep update(BookKeep bookKeep, BookKeepVm bookKeepVm) {
        bookKeep.setBookKeepDate(bookKeepVm.getBookKeepDate());
        bookKeep.setType(bookKeepVm.getType());
        bookKeep.setCategory(bookKeepVm.getCategory());
        bookKeep.setPrice(bookKeepVm.getPrice());
        bookKeep.setRemark(bookKeepVm.getRemark());
        bookKeep.setUpdateTime(LocalDateTime.now());

        return bookKeepRepository.save(bookKeep);
    }

    public List<BookKeepDto> getFilteredBookKeeps(User user, Integer year, Integer month) {
        List<BookKeep> list = bookKeepRepository.findByUser(user).stream()
                .filter(book -> {
                    if (year != null && book.getBookKeepDate().getYear() != year) {
                        return false;
                    }
                    if (month != null && book.getBookKeepDate().getMonthValue() != month) {
                        return false;
                    }
                    return true;
                }).toList();

        return list.stream().map(BookKeepDto::new).collect(Collectors.toList());
    }

    public Statistics getStatistics(Integer id) {
        Optional<User> userOpt = userRepository.findById(id);

        if (!userOpt.isPresent()){
            throw new RuntimeException("用户不存在");
        }
        User user = userOpt.get();
        List<BookKeep> outcomeBookKeeps = bookKeepRepository.findByUserAndType(user, 0);
        List <BookKeep> incomeBookKeeps = bookKeepRepository.findByUserAndType(user, 1);

        Integer outcome = 0 , income = 0;
        //  计算支出和收入
        for(BookKeep bookKeep : outcomeBookKeeps){
            outcome += bookKeep.getPrice().intValue();
        }
        for(BookKeep bookKeep : incomeBookKeeps){
            income += bookKeep.getPrice().intValue();
        }
        System.out.println(outcome + income);
        return new Statistics(outcome, income);
    }
}
